package com.zHan.controller;

import com.zHan.pojo.Category;
import com.zHan.pojo.Result;
import com.zHan.service.CategoryService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: CategoryController
 * Description:
 * date: 2023/11/9 0009 15:55
 *
 * @author zHan
 */
@RestController
@RequestMapping("/category")
@Validated
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        return categoryService.add(category);
    }

    /**
     * 文章分类列表
     *
     * @return
     */
    @GetMapping
    public Result list() {
        return categoryService.list();
    }

    /**
     * 获取文章分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result detail(@NotNull Integer id) {
        return categoryService.findById(id);
    }

    /**
     * 更新文章分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        return categoryService.update(category);
    }

    /**
     * 删除文章分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(@NotNull Integer id) {
        return categoryService.delete(id);
    }

}
